﻿function MeunTree(treeID, Tabs, MenuID, data, DesktopName, DesktopUrl) {
    treeID = '#' + treeID;
    Tabs = '#' + Tabs;
    MenuID = '#' + MenuID;


    var Desktop = function ()//添加默认打开的桌面页
    {
        var content = '<div style="overflow:hidden;width:100%;height:100%;"><iframe scrolling="auto" frameborder="0"  src="' + DesktopUrl + '" style="width:100%;height:100%;"></iframe></div>';
        $(Tabs).tabs('add', {
            title: DesktopName,
            content: content,
            loadingMessage: '加载中...',
            closable: false
        });
    };

    $(document).ready(function () {
        if (DesktopName != "" && DesktopName != undefined) Desktop();
        $(treeID).tree({
            data: data,
            lines: true,
            animate: true,
            onClick: function (node) {
                var node = $(treeID).tree('getSelected');
                switch (node.attributes.target) {
                    case "ClientFrame":
                        if (node.attributes.url != "" && node.attributes.url != "about:blank") {
                            var title = node.text;
                            addIframe(title, node.attributes.url);
                            break;
                        }
                    case "_blank":
                        if (node.attributes.url != "" && node.attributes.url != "about:blank") {
                            window.open(node.attributes.url);
                            break;
                        }
                }
            }
        });
    });

    var addIframe = function (title, url) {
        if ($(Tabs).tabs('exists', title)) {
            $(Tabs).tabs('select', title);
            var iframe = $(Tabs).tabs('getTab', title).find('iframe')[0];
            iframe.src = url;
        }
        else {
            var content = '<div style="overflow:hidden;width:100%;height:100%;"><iframe scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:100%;"></iframe></div>';
            $(Tabs).tabs('add', {
                title: title,
                content: content,
                loadingMessage: '加载中...',
                closable: true
            });
        }
    };

};

/**===右键菜单处理===**/
var onlyOpenTitle = ["桌面", "报警监测"];//不能关闭的tabs
$(function () {
    //绑定tabs的右键菜单
    $("#tabs").tabs({
        onContextMenu: function (e, title) {
            e.preventDefault();
            $('#menu').menu('show', {
                left: e.pageX,
                top: e.pageY
            }).data("tabTitle", title);
        }
    });
    ////关闭事件
    $('#menu').menu({
        onClick: function (item) {
            closeTab(item.id);
        }
    });
});

function closeTab(action) {
    if (action == undefined) {
        return;
    }
    var alltabs = $('#tabs').tabs('tabs');//全部标签
    var selectTab = $('#tabs').tabs('getSelected');//选中的标签
    var Item = [];//保存tabs的数组
    for (var i = 0; i < alltabs.length; i++) {
        var title = alltabs[i].panel('options').title;
        Item.push(title);
    }

    switch (action) {
        case "refresh"://刷新
            {
                var iframe = $(selectTab.panel('options').content);
                var src = $($(selectTab.panel('options').content)[0].innerHTML)[0].src;
                $('#tabs').tabs('update', {
                    tab: selectTab,
                    options: {
                        content: '<iframe scrolling="no" frameborder="0"  src="' + src + '" style="width:100%;height:100%;"></iframe>'
                    }
                });

                break;
            }
        case "close"://关闭选中标签
            {
                var title = selectTab.panel('options').title;
                if (onlyOpenTitle.indexOf(title) < 0) {
                    $('#tabs').tabs('close', title);
                }
                else {
                    $.messager.alert('提示信息', title + "不能关闭！", 'info');
                }
                break;
            }
        case "closeall"://关闭全部标签
            {
                for (var i = 0; i < Item.length; i++) {
                    var title = Item[i];
                    if (onlyOpenTitle.indexOf(title) < 0) {
                        $('#tabs').tabs('close', title);
                    }
                }
                break;
            }
        case "closeother"://关闭其它标签
            {
                for (var i = 0; i < Item.length; i++) {
                    var title = Item[i];
                    if (onlyOpenTitle.indexOf(title) < 0 && title != selectTab.panel('options').title) {
                        $('#tabs').tabs('close', title);
                    }
                }
                break;
            }
        case "closeright"://关闭右侧标签
            {
                var tabIndex = $('#tabs').tabs('getTabIndex', selectTab);
                if (tabIndex == Item.length - 1) {
                    $.messager.alert('提示信息', "已经是最后一个了！", 'info');
                    return false;
                }
                for (var i = 0; i < Item.length; i++) {
                    if (i > tabIndex) {
                        var title = Item[i];
                        if (onlyOpenTitle.indexOf(title) < 0) {
                            $('#tabs').tabs('close', title);
                        }
                    }
                }
                break;
            }
        case "closeleft"://关闭左侧标签
            {
                var tabIndex = $('#tabs').tabs('getTabIndex', selectTab);
                if (tabIndex == 1) {
                    $.messager.alert('提示信息', "已经是第一个了！", 'info');
                    return false;
                }
                for (var i = 0; i < Item.length; i++) {
                    if (i < tabIndex) {
                        var title = Item[i];
                        if (onlyOpenTitle.indexOf(title) < 0) {
                            $('#tabs').tabs('close', title);
                        }
                    }
                }
                break;
            }
        case "exit"://退出
            {
                $('#menu').menu('hide');
                break;
            }
    }
}